#include "BookmarksDockWidget.h"
#include "ui_BookmarksDockWidget.h"

#include "BookmarksManager.h"
#include "BookmarksModel.h"
#include "BookmarkNode.h"

#include <QMenu>
#include <QDebug>
#include <QUrl>

BookmarksDockWidget::BookmarksDockWidget(BookmarksManager *manager, QWidget *parent) :
    m_bookmarksManager(manager),
    QDockWidget(parent),
    ui(new Ui::BookmarksDockWidget)
{
    ui->setupUi(this);

    ui->tree->setUniformRowHeights(true);
    ui->tree->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tree->setSelectionMode(QAbstractItemView::ContiguousSelection);
    ui->tree->setTextElideMode(Qt::ElideMiddle);
    m_bookmarksModel = m_bookmarksManager->bookmarksModel();

    ui->tree->setModel(m_bookmarksModel);
    ui->tree->setDragDropMode(QAbstractItemView::InternalMove);

    ui->tree->setAlternatingRowColors(true);
    QFontMetrics fm(font());
    int header = fm.width(QLatin1Char('m')) * 35;
    ui->tree->header()->resizeSection(0, header);
    header = fm.width(QLatin1Char('m')) * 35;
    ui->tree->header()->resizeSection(1, header);
    header = fm.width(QLatin1Char('m')) * 5;
    ui->tree->header()->resizeSection(2, header);
    ui->tree->header()->setStretchLastSection(true);

    ui->tree->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(ui->tree, SIGNAL(customContextMenuRequested(const QPoint &)),
            this, SLOT(customContextMenuRequested(const QPoint &)));
}

BookmarksDockWidget::~BookmarksDockWidget()
{
    delete ui;
}

void BookmarksDockWidget::customContextMenuRequested(const QPoint &pos)
{
    QMenu menu;
    QModelIndex index = ui->tree->indexAt(pos);
    index = index.sibling(index.row(), 0);

    if ( !index.parent().isValid())
        return;

    bool isLink = index.isValid() && !ui->tree->model()->hasChildren(index);

    if ( isLink )
    {
        menu.addAction(tr("Открыть"), this, SLOT(open()));
        menu.addSeparator();
        menu.addAction(QIcon(":icons/run.png"), tr("Запустить с текущим шаблоном"),
                       this, SLOT(process( index   /* , BookmarksDockWidget::NodeType NT_IsLink, BookmarksDockWidget::TemlateType TT_CurrentTemplate */)));
        menu.addAction(QIcon(":icons/run_dir.png"), tr("Запустить с шаблоном..."),
                       this, SLOT(process( index/*, NT_IsLink, TT_UserSetTemplate*/ )));
    }
    else    //Dir
    {
        menu.addAction(QIcon(":icons/run_dir.png"), tr("Запустить с текущим шаблоном"),
                       this, SLOT(process( index/*, NT_IsDir, TT_CurrentTemplate*/ )));
        menu.addAction(QIcon(":icons/run_dir.png"), tr("Запустить с шаблоном..."),
                       this, SLOT(process( index/*, NT_IsDir, TT_CurrentTemplate*/ )));
    }

    menu.addSeparator();
    menu.addAction(tr("Удалить"), ui->tree, SLOT(removeOne()));
    menu.exec(QCursor::pos());
}

void BookmarksDockWidget::open()
{
    QModelIndex index = ui->tree->currentIndex();
    if (!index.parent().isValid())
        return;
    emit openUrl(index.sibling(index.row(), 1).data(BookmarksModel::UrlRole).toUrl());
}

void BookmarksDockWidget::process(const QModelIndex &index/*, BookmarksDockWidget::NodeType nodeType, BookmarksDockWidget::TemlateType templateType*/)
{
//    if (nodeType == NT_IsLink)
//    {
        emit openUrl(index.sibling(index.row(), 1).data(BookmarksModel::UrlRole).toUrl());
//    }
}
